package com.demo.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect //声明是一个切面类
public class LogAop {


    private Logger logger = LoggerFactory.getLogger(LogAop.class);

    @Pointcut("execution(public * com.demo.service.*.*(..)) || @annotation(com.demo.log.DemoLog)")
    public void aoplog(){}
    //Aop通知 前置通知，后置通知，异常通知，环绕通知，返回后通知

    @Before("aoplog()")
    public void doBefore(JoinPoint joinPoint){
//      参数
        Object[] args = joinPoint.getArgs();
//      方法
        Signature signature = joinPoint.getSignature();
        logger.info("进入方法：{} , 参数为：{}" ,signature , args);
    }

    @AfterReturning(pointcut = "aoplog()" ,returning = "obj")
    public void doAfterReturning(Object obj) throws Throwable{
        //打印返回值
        logger.info("方法执行成功，返回值：{}" , JSONObject.toJSONString(obj));
    }
}
